package com.billard.controller;

import com.billard.websocket.TableStatusPublisher;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/websocket")
@Tag(name = "广播管理测试接口", description = "websocket")
public class TestWebSocketController {


    private final TableStatusPublisher publisher;

    public TestWebSocketController(TableStatusPublisher publisher) {
        this.publisher = publisher;
    }

    // 广播球台状态
    @Operation(summary = "广播球台状态", description = "广播所有球台的最新状态")
    @GetMapping("/sendTableStatus")
    public String sendTableStatus() {
        publisher.publishTableStatusChange(1L); // 传入球台ID
        return "Table status message sent!";
    }

    // 广播预约提醒
    @Operation(summary = "广播预约提醒", description = "广播所有预约的提醒")
    @GetMapping("/sendReservationReminder")
    public String sendReservationReminder() {
        publisher.publishReservationReminder("您有新的预约提醒！");
        return "Reservation reminder sent!";
    }

    // 给指定用户发送消息
    @Operation(summary = "给指定用户发送消息", description = "给指定用户发送消息")
    @GetMapping("/sendToUser")
    public String sendToUser() {
        publisher.publishToUser("user1", "这是私有消息");
        return "Message sent to user1!";
    }

    @MessageMapping("/sendTableStatus")
    public void handleSendTableStatus(String tableId) {
        publisher.publishTableStatusChange(Long.valueOf(tableId));
    }

    @MessageMapping("/sendReservationReminder")
    public void handleSendReservationReminder(String message) {
        publisher.publishReservationReminder(message);
    }

    @MessageMapping("/sendSystemNotification")
    public void handleSendSystemNotification(String message) {
        publisher.publishSystemNotification(message);
    }

    @MessageMapping("/sendToUser")
    public void handleSendToUser(Map<String, String> payload) {
        String username = payload.get("username");
        String msg = payload.get("payload");
        publisher.publishToUser(username, msg);
    }
}
